<?php 

class etat_financier_ctr extends CI_Controller {  
       
	    function __construct()
    {
        parent::__construct();
         $this->load->library("pagination");
		$this->load->helper(array('form', 'url'));
        $this->load->library('Convert_ctr');
				

    }
	  
	 function index()
	{
		
		 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		
	   
	   $tab_ch=$this->etat_chapitres();
	    $nom_chapitres= $this->charticle_mdl->get_chapitres();
		$budget=$this->etat_financier_mdl->get_all();
		$data["chapitres"] = $tab_ch[1];
		$data["chapitres_payer"] = $tab_ch[2];
		$data['nom_chapitres']=$nom_chapitres;
		if($budget){
			$data["result"] = $budget;
       		$data['titre']='الوضعية المالية لسنة: <br>'. $year;
		
		  $this->load->view('etat_financier_vew',$data);  // send the result from the model to the view 
		  }else{
		  	 $this->load->view('definition_budget_vew',$data);  // send the result from the model to the view 
		  }
	
	}
	
	 function etat_chapitres()
	{
		
		 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	
	  
	  for($i=11;$i<20;$i++){
	  	 $prix_bc=0;
	   $prix_conv=0;
	   $prix_bc_payer=0;
	   $prix_conv_payer=0;
	  	// calcule montant des bc engager
	  	 $inf[$i] = $this->etat_financier_mdl->calcule_chapitre($i);
		 if($inf[$i]){
		 	$tmp=$inf[$i];
			$prix_bc=0.00;
		 	for($j=0;$j<sizeof($inf[$i]);$j++){
				$prix_bc=$prix_bc+($tmp[$j]->bc_produit_pu*$tmp[$j]->quantite);
			}
			}
			// calcule montant de convention engager
			 $info[$i] = $this->etat_financier_mdl->calcule_chapitre_convention($i);
		 if($info[$i]){
		 	$tmpo=$info[$i];
			$prix_conv=0.00;
		 	for($j=0;$j<sizeof($info[$i]);$j++){
				$prix_conv=$prix_conv+($tmpo[$j]->pu_convention_produit*$tmpo[$j]->quantite);
			}
			
		 }
		 
		 	// calcule montant des bc payer
	  	 $inf_p[$i] = $this->etat_financier_mdl->calcule_chapitre_payer($i);
		 if($inf_p[$i]){
		 	$tmp_p=$inf_p[$i];
			$prix_bc_payer=0.00;
		 	for($j=0;$j<sizeof($inf_p[$i]);$j++){
				$prix_bc_payer=$prix_bc_payer+($tmp_p[$j]->bc_produit_pu*$tmp_p[$j]->quantite);
			}
			}
			
		 //calcule montant des bc payer		 
		  $info_p[$i] = $this->etat_financier_mdl->calcule_chapitre_convention_payer($i);
		 if($info_p[$i]){
		 	$tmpo_p=$info_p[$i];
			$prix_conv_payer=0.00;
		 	for($j=0;$j<sizeof($info_p[$i]);$j++){
				$prix_conv_payer=$prix_conv_payer+($tmpo_p[$j]->pu_convention_produit*$tmpo_p[$j]->quantite);
			}
			
		 }//else $tab[$i]=0;
		 $prix=$prix_bc+$prix_conv;
		 $prix_payer=$prix_bc_payer+$prix_conv_payer;
			$tab[$i]=$prix;
			$tab_payer[$i]=$prix_payer;
	  }
       
$g=0;
	$tab_t[1]=$tab;
	$tab_t[2]=$tab_payer;	
		
	return $tab_t;	
	
	}

   function detaille_chapitre($ch){
   	$this->load->model('charticle_mdl');
   	$v=$this->input->post($ch);
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	
	$nom_articles=$this->charticle_mdl->get_articles($ch);

		for($i=0;$i<sizeof($nom_articles);$i++){
			$compt=$i+1;
			// calcule montant bc engager articles
			$tmp=$this->etat_financier_mdl->calcule_article($ch,$compt);
			$prix_bc=0;
			if($tmp){
				
			for($j=0;$j<sizeof($tmp);$j++){
			$prix_bc=$prix_bc+($tmp[$j]->bc_produit_pu * $tmp[$j]->quantite);	
			}
			}
			
			// calcule montant bc payer articles
			$tmp_p=$this->etat_financier_mdl->calcule_article_payer($ch,$compt);
			$prix_bc_payer=0;
			if($tmp_p){
				
			for($j=0;$j<sizeof($tmp_p);$j++){
			$prix_bc_payer=$prix_bc_payer+($tmp_p[$j]->pu_convention_produit * $tmp_p[$j]->quantite);	
			}
			}
			
	
		// calcule montant convention engager articles
		$tmpo=$this->etat_financier_mdl->calcule_article_convention($ch,$compt);
			$prix_conv=0;
			if($tmpo){
				
			for($j=0;$j<sizeof($tmpo);$j++){
			$prix_conv=$prix_conv+($tmpo[$j]->pu_convention_produit * $tmpo[$j]->quantite);	
			}
			}
			
			// calcule montant convention payer articles
		$tmpo_p=$this->etat_financier_mdl->calcule_article_convention_payer($ch,$compt);
			$prix_conv_payer=0;
			if($tmpo_p){
				
			for($j=0;$j<sizeof($tmpo_p);$j++){
			$prix_conv_payer=$prix_conv_payer+($tmpo_p[$j]->pu_convention_produit * $tmpo_p[$j]->quantite);	
			}
			}
			
		$comsomer[$i]=$prix_bc+$prix_conv;
		$payer[$i]=$prix_bc_payer+$prix_conv_payer;
		
	}
	
	$budget_init_ch=$this->etat_financier_mdl->get_articles_solde($ch,0);
	$budget_supl_ch=$this->etat_financier_mdl->get_articles_solde($ch,1);
	
	$m=0;
	$data['payer']=$payer;
	$data['consommer']=$comsomer;
	$data['nom_art']=$nom_articles;
	$data['budget_init_ch']=$budget_init_ch;
	$data['budget_supl_ch']=$budget_supl_ch;
	$data['num_ch']=$ch;
	
	  $this->load->view('etat_financier_chapitre_imprimer_vew',$data);  // send the result from the model to the view 
   }	
   
    function nouveau_budget_initial(){
   
	$budget=$this->input->post('budget');
   	$ch_11=$this->input->post(11);
	$ch_12=$this->input->post(12);
	$ch_13=$this->input->post(13);
	$ch_14=$this->input->post(14);
	$ch_15=$this->input->post(15);
	$ch_16=$this->input->post(16);
	$ch_17=$this->input->post(17);
	$ch_18=$this->input->post(18);
	$ch_19=$this->input->post(19);
	
	// on verifie la distribution de budget 
	$total_ch=$ch_11+$ch_12+$ch_13+$ch_14+$ch_15+$ch_16+$ch_17+$ch_18+$ch_19;
	if($total_ch!=$budget){ // si la division de budget ne correspond pas au budget
		 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		
		$tab_saisie['ch_11']=$ch_11;
		$tab_saisie['ch_12']=$ch_12;
		$tab_saisie['ch_13']=$ch_13;
		$tab_saisie['ch_14']=$ch_14;
		$tab_saisie['ch_15']=$ch_15;
		$tab_saisie['ch_16']=$ch_16;
		$tab_saisie['ch_17']=$ch_17;
		$tab_saisie['ch_18']=$ch_18;
		$tab_saisie['ch_19']=$ch_19;
	   
	   $tab_ch=$this->etat_chapitres();
	    $nom_chapitres= $this->charticle_mdl->get_chapitres();
		$data['nom_chapitres']=$nom_chapitres;
		$data['saisie']=$tab_saisie;
		$data['budget_correspond']=$total_ch-$budget;
		$data['budget']=$budget;
		  	 $this->load->view('definition_budget_vew',$data);  // send the result from the model to the view 
	}else{
		
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$nom_articles=$this->etat_financier_mdl->nouveau_budget_init($ch_11,$ch_12,$ch_13,$ch_14,$ch_15,$ch_16,$ch_17,$ch_18,$ch_19,$budget);
	$this->index();
	}
	
   }	
   
    function nouveau_budget_suplementaire(){
    $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	    $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
	   $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
	   
	   $tab_ch=$this->etat_chapitres();
	    $nom_chapitres= $this->charticle_mdl->get_chapitres();
		$budget=$this->etat_financier_mdl->get_all();
		$data["chapitres"] = $tab_ch;
		$data['nom_chapitres']=$nom_chapitres;
		
		
		  	 $this->load->view('definition_budget_suplementaire_vew',$data);  // send the result from the model to the view 
		 
	
   }	
   
     function nouveau_budget_init_chapitre($num_ch){
   
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$nom_articles=$this->etat_financier_mdl->get_articles($num_ch);
	$data['articles']=$nom_articles;
	$data['titre']='تقسيم الميزانية لقسم '.$nom_articles[0]->chapitre;
	 $this->load->view('definition_budget_article_vew',$data);  // send the result from the model to the view 
		
   }	
   
    function modifier_budget_init_chapitre($num_ch){
   
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$nom_articles=$this->etat_financier_mdl->get_articles($num_ch);
	$ch='ch_'.$num_ch;
	$budget_ch=$this->etat_financier_mdl->get_budget_ch($ch);
	$budget_ch=$budget_ch[0]->$ch;
	$data['articles']=$nom_articles;
	$data['budget_ch']=$budget_ch;
	$data['titre']='تقسيم الميزانية لقسم '.$nom_articles[0]->chapitre;
	 $this->load->view('modifier_budget_article_vew',$data);  // send the result from the model to the view 
		
   }
													  
    function modifier_budget_supl_chapitre($num_ch){
   
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$nom_articles=$this->etat_financier_mdl->get_articles_supl($num_ch);
	$ch='ch_'.$num_ch;
	$budget_ch=$this->etat_financier_mdl->get_budget_ch($ch);
	$budget_ch=$budget_ch[1]->$ch;
	$data['articles']=$nom_articles;
	$data['budget_ch']=$budget_ch;
	$data['titre']='تقسيم الميزانية لقسم '.$nom_articles[0]->chapitre;
	 $this->load->view('modifier_budget_supl_article_vew',$data);  // send the result from the model to the view 
		
   }	
   
    function valider_modife_budget_init_chapitre($num_ch,$nbr_enreg,$budget_ch){
   
	$montant=0;
    for($i=0;$i<$nbr_enreg;$i++){
		$tab[$i]=$this->input->post($i);
		$montant=$montant+$this->input->post($i);
	}
	
	if($montant==$budget_ch){
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		 $today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
				
	$this->etat_financier_mdl->modifier_budget_init_chapitre($tab,$num_ch,$date);
	 echo(' <script>
	   window.alert(" تمت العملية بنجاح");
	   </script> ');
	$this->modifier_budget_initial();
	}else{
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$nom_articles=$this->etat_financier_mdl->get_articles($num_ch);
	$ch='ch_'.$num_ch;
	$budget_ch=$this->etat_financier_mdl->get_budget_ch($ch);
	$budget_ch=$budget_ch[0]->$ch;
	$data['articles']=$nom_articles;
	$data['budget_ch']=$budget_ch;
	$data['ecart']=$budget_ch-$montant;
	$data['tab']=$tab;
	$data['titre']='تقسيم الميزانية لقسم '.$nom_articles[0]->chapitre;
	 $this->load->view('modifier_budget_article_vew',$data);  // send the result from the model to the view 
		
	}
	

	
   }	
		
	function valider_modife_budget_supl_chapitre($num_ch,$nbr_enreg,$budget_ch){
   
	$montant=0;
    for($i=0;$i<$nbr_enreg;$i++){
		$tab[$i]=$this->input->post($i);
		$montant=$montant+$this->input->post($i);
	}
	
	if($montant==$budget_ch){
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		$this->load->model('engagement_mdl');
		$tab_id_art=$this->engagement_mdl->get_article_id($num_ch);
		for($i = 0; $i < count($tab_id_art); $i++){
			$existe=$this->engagement_mdl->existe_budget_sup($tab_id_art[$i]->id_article);
			if($existe){
				$this->etat_financier_mdl->modifier_budget_supl_art($tab_id_art[$i]->idarticle,$tab[$i]);
			}else{
				
				$today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
				$tab_num_eng=$this->engagement_mdl->get_engagement_numero($tab_id_art[$i]->id_article);
				if($tab_num_eng)$num_eng=$tab_num_eng[0]->numero +1;
				else $num_eng=2;
				$this->etat_financier_mdl->ajout_budget_supl_art($tab_id_art[$i]->id_article,$tab[$i],$date,$num_eng);
			}
			
			
		}
		//$tab_num_eng=$this->engagement_mdl->get_engagement_numero();
		
//	$this->etat_financier_mdl->modifier_budget_supl_chapitre($tab,$num_ch);
	 echo(' <script>
	   window.alert(" تمت العملية بنجاح");
	   </script> ');
	$this->modifier_budget_suplementaire();
	}else{
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$nom_articles=$this->etat_financier_mdl->get_articles($num_ch);
	$ch='ch_'.$num_ch;
	$budget_ch=$this->etat_financier_mdl->get_budget_ch($ch);
	$budget_ch=$budget_ch[1]->$ch;
	$data['articles']=$nom_articles;
	$data['budget_ch']=$budget_ch;
	$data['ecart']=$budget_ch-$montant;
	$data['tab']=$tab;
	$data['titre']='تقسيم الميزانية لقسم '.$nom_articles[0]->chapitre;
	 $this->load->view('modifier_budget_supl_article_vew',$data);  // send the result from the model to the view 
		
	}
	

	
   }	
    function nouveau_budget_supl_chapitre($num_ch){
   
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$nom_articles=$this->etat_financier_mdl->get_articles_supl($num_ch);
	$data['articles']=$nom_articles;
	$data['titre']='تقسيم الميزانية الاضافية للقسم '.$nom_articles[0]->chapitre;
	 $this->load->view('definition_budget_supl_article_vew',$data);  // send the result from the model to the view 
		
   }	
   
   
     function ajout_budget_suplementaire(){
   
	$budget=$this->input->post('budget');
   	$ch_11=$this->input->post(11);
	$ch_12=$this->input->post(12);
	$ch_13=$this->input->post(13);
	$ch_14=$this->input->post(14);
	$ch_15=$this->input->post(15);
	$ch_16=$this->input->post(16);
	$ch_17=$this->input->post(17);
	$ch_18=$this->input->post(18);
	$ch_19=$this->input->post(19);
	$initaile=1;
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$nom_articles=$this->etat_financier_mdl->nouveau_budget_supl($ch_11,$ch_12,$ch_13,$ch_14,$ch_15,$ch_16,$ch_17,$ch_18,$ch_19,$budget);
	$this->index();
	
   }	
   
      function ajout_budget_init_chapitre($num_ch,$nbr_enreg){
   
	$budget=$this->input->post('budget');
    for($i=0;$i<$nbr_enreg;$i++){
		$tab[$i]=$this->input->post($i);
	}
	
	$initaile=1;
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	//$this->load->model('engagement_mdl'); 
	$this->etat_financier_mdl->nouveau_budget_init_chapitre($tab,$num_ch);
	//$this->engagement_mdl->ajout_engagement($article,$nature,$date,$numero);
	$this->modifier_budget_initial();

	
   }	
   
   
																	
		 function ajout_budget_supl_chapitre($num_ch,$nbr_enreg){
   
	$budget=$this->input->post('budget');
    for($i=0;$i<$nbr_enreg;$i++){
		$tab[$i]=$this->input->post($i);
	}
	
	$initaile=1;
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$this->etat_financier_mdl->nouveau_budget_supl_chapitre($tab,$num_ch);
	$this->modifier_budget_suplementaire();

	
   }																
   
    function modifier_budget_initial(){
	 $this->load->library('session');
	  $tab_ch=$this->etat_chapitres();//etat financier de chaque chapitre
	    $nom_chapitres= $this->charticle_mdl->get_chapitres();
		$budget=$this->etat_financier_mdl->get_all();
		//$data["chapitres"] = $tab_ch;

		$data["etat_ch"] = $budget[0];	
		
		$data['nom_chapitres']=$nom_chapitres;
	 $year=$this->session->userdata('annee');
	$data['titre']='تحديث تقسيم الميزانية لسنة: <br>'. $year;
    $this->load->view('modifier_budget_vew',$data);  // send the result from the model to the view 
	
   }	
   
     function modifier_budget_suplementaire(){
	 $this->load->library('session');
	  $tab_ch=$this->etat_chapitres();//etat financier de chaque chapitre
	    $nom_chapitres= $this->charticle_mdl->get_chapitres();
		$budget=$this->etat_financier_mdl->get_all();
		//$data["chapitres"] = $tab_ch;

		$data["etat_ch"] = $budget[1];	
		
		$data['nom_chapitres']=$nom_chapitres;
	 $year=$this->session->userdata('annee');
	$data['titre']='تحديث تقسيم الميزانية الاضافية لسنة: <br>'. $year;
    $this->load->view('modifier_budget_suplementaire_vew',$data);  // send the result from the model to the view 
	
   }	
													
     function valider_modifier_budget_initial($id){
   
	$budget_modifie=$this->input->post('budget');
   	$ch_11=$this->input->post(11);
	$ch_12=$this->input->post(12);
	$ch_13=$this->input->post(13);
	$ch_14=$this->input->post(14);
	$ch_15=$this->input->post(15);
	$ch_16=$this->input->post(16);
	$ch_17=$this->input->post(17);
	$ch_18=$this->input->post(18);
	$ch_19=$this->input->post(19);
	
	// on verifie la distribution de budget 
	$total_ch=$ch_11+$ch_12+$ch_13+$ch_14+$ch_15+$ch_16+$ch_17+$ch_18+$ch_19;
	if($total_ch!=$budget_modifie){ // si la division de budget ne correspond pas au budget
		 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		
		$tab_saisie['ch_11']=$ch_11;
		$tab_saisie['ch_12']=$ch_12;
		$tab_saisie['ch_13']=$ch_13;
		$tab_saisie['ch_14']=$ch_14;
		$tab_saisie['ch_15']=$ch_15;
		$tab_saisie['ch_16']=$ch_16;
		$tab_saisie['ch_17']=$ch_17;
		$tab_saisie['ch_18']=$ch_18;
		$tab_saisie['ch_19']=$ch_19;
	   
	   $this->load->library('session');
	  $tab_ch=$this->etat_chapitres();//etat financier de chaque chapitre
	    $nom_chapitres= $this->charticle_mdl->get_chapitres();
		$budget=$this->etat_financier_mdl->get_all();
		//$data["chapitres"] = $tab_ch;

		$data["etat_ch"] = $budget[0];	
		$data['budget_correspond']=$total_ch-$budget_modifie;
		$data['nom_chapitres']=$nom_chapitres;
		$data['saisie']=$tab_saisie;
		$data['budget_modifie']=$budget_modifie;
	 $year=$this->session->userdata('annee');
	$data['titre']='تحديث تقسيم الميزانية لسنة: <br>'. $year;
    $this->load->view('modifier_budget_vew',$data);  // send the result from the model to the view 
		}else{
		
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$this->etat_financier_mdl->modifier_budget_init($ch_11,$ch_12,$ch_13,$ch_14,$ch_15,$ch_16,$ch_17,$ch_18,$ch_19,$budget_modifie,$id);
	 echo(' <script>
	   window.alert(" تمت العملية بنجاح");
	   </script> ');
	$this->index();
	}
	
	
	
   }	
				
   
     function valider_modifier_budget_suplementaire($id){
   
	$budget_modifie=$this->input->post('budget');
   	$ch_11=$this->input->post(11);
	$ch_12=$this->input->post(12);
	$ch_13=$this->input->post(13);
	$ch_14=$this->input->post(14);
	$ch_15=$this->input->post(15);
	$ch_16=$this->input->post(16);
	$ch_17=$this->input->post(17);
	$ch_18=$this->input->post(18);
	$ch_19=$this->input->post(19);
	
	$total_ch=$ch_11+$ch_12+$ch_13+$ch_14+$ch_15+$ch_16+$ch_17+$ch_18+$ch_19;
	
	if($total_ch!=$budget_modifie){ // si la division de budget ne correspond pas au budget
		 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		
		$tab_saisie['ch_11']=$ch_11;
		$tab_saisie['ch_12']=$ch_12;
		$tab_saisie['ch_13']=$ch_13;
		$tab_saisie['ch_14']=$ch_14;
		$tab_saisie['ch_15']=$ch_15;
		$tab_saisie['ch_16']=$ch_16;
		$tab_saisie['ch_17']=$ch_17;
		$tab_saisie['ch_18']=$ch_18;
		$tab_saisie['ch_19']=$ch_19;
	   
	   $this->load->library('session');
	  $tab_ch=$this->etat_chapitres();//etat financier de chaque chapitre
	    $nom_chapitres= $this->charticle_mdl->get_chapitres();
		$budget=$this->etat_financier_mdl->get_all();
		//$data["chapitres"] = $tab_ch;

		$data["etat_ch"] = $budget[1];	
		$data['budget_correspond']=$total_ch-$budget_modifie;
		$data['nom_chapitres']=$nom_chapitres;
		$data['saisie']=$tab_saisie;
		$data['budget_modifie']=$budget_modifie;
	 $year=$this->session->userdata('annee');
	$data['titre']='تحديث تقسيم الميزانية لسنة: <br>'. $year;
    $this->load->view('modifier_budget_suplementaire_vew',$data);  // send the result from the model to the view 
	
	
		}else{
		
	$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$this->etat_financier_mdl->modifier_budget_supl($ch_11,$ch_12,$ch_13,$ch_14,$ch_15,$ch_16,$ch_17,$ch_18,$ch_19,$budget_modifie,$id);
	 echo(' <script>
	   window.alert(" تمت العملية بنجاح");
	   </script> ');
	$this->index();
	}
	
	
   }	
							
							
	 function transfert(){
   
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		
		$les_transfert= $this->etat_financier_mdl->get_transfert(); // call the method filtera inside the class welcome_mdl
	    if($les_transfert){ // si existe des transfert on affiche la liste
		     $data['result']=$les_transfert;
			 $this->load->view('transfert_liste_vew',$data);  // send the result from the model to the view 	
		}else{
	       	  $this->load->view('transfert_ajout_vew');  // send the result from the model to the view 		
		}	   	
	
   }	
   
   function detaille_transfert($id_transfert){
   
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		
		$les_transfert= $this->etat_financier_mdl->get_detaille_transfert($id_transfert); // call the method filtera inside the class welcome_mdl
	   
		     $data['result']=$les_transfert;
			 $this->load->view('transfert_detaille_vew',$data);  // send the result from the model to the view 	
	
	
   }	
   
    function imprimer_transfert($id_transfert){
   
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
	$les_transfert= $this->etat_financier_mdl->get_detaille_transfert($id_transfert); // call the method filtera inside the class welcome_mdl
	
	$id_art_sourc=$les_transfert[0]->id_art_source;
	$id_art_dest=$les_transfert[0]->id_art_dest;
	   
	   $budget_art_source=$this->etat_financier_mdl->get_etat_budget_art($id_art_sourc);
	   $budget_art_dest=$this->etat_financier_mdl->get_etat_budget_art($id_art_dest);
	   
	   
		     $data['result']=$les_transfert;
			 $data['budget_art_source']=$budget_art_source;
			 $data['budget_ar_dest']=$budget_art_dest;
			 $this->load->view('transfert_imprimer_vew',$data);  // send the result from the model to the view 	
	
   }	
   
     function ajout_transfert(){
   
		
	       	  $this->load->view('transfert_ajout_vew');  // send the result from the model to the view 		
	
   }	
   
     function supprimer_transfert($id_transfert,$art_source,$art_dest,$montant){
   
		$this->load->model('etat_financier_mdl'); // load the file welcome_mdl.php
		
		
	   $etat_source=$this->etat_financier_mdl->etat_transfert($art_source);
	   $compt_source=$etat_source[0]->transfert-1;
	   $solde_source=$etat_source[0]->solde+$montant;
	   
		$etat_dest=$this->etat_financier_mdl->etat_transfert($art_dest);
		$compt_dest=$etat_dest[0]->transfert+1;
		$solde_dest=$etat_dest[0]->solde-$montant;
		
		$les_transfert= $this->etat_financier_mdl->supprimer_transfert($id_transfert,$art_source,$art_dest,$solde_source,$solde_dest,$compt_source,$compt_dest); 
		
		     $this->transfert();
	
   }	
   
   
   
		
	   function valider_transfert(){
   
	$source=$this->input->post('source');
   	$destination=$this->input->post('destination');
	$montant=$this->input->post('montant');
	$numero=$this->input->post('numero');
	
	
	$this->load->model('etat_financier_mdl');
	$this->load->model('charticle_mdl'); 
	$this->load->model('engagement_mdl'); 
	
	$ch_source=$this->charticle_mdl->get_num_chapitre($source);
	$ch_dest=$this->charticle_mdl->get_num_chapitre($destination);
	if($ch_source[0]->id_chapitre_fk==$ch_dest[0]->id_chapitre_fk){ // test si les articles sont de meme chapitre
		$etat_source=$this->etat_financier_mdl->etat_transfert($source);
		$etat_dest=$this->etat_financier_mdl->etat_transfert($destination);
		
		if($etat_dest AND $etat_source){
			
			 $numero_bon=TRUE;
		  if($numero){
		  	$tab_num=$this->etat_financier_mdl->existe_numero_transfert();
			if($tab_num)$numero_bon=FALSE;
			else $numero_bon=TRUE;
		  }else{
		  	$tab_num=$this->etat_financier_mdl->get_numero_transfert();
			if($tab_num)$numero=$tab_num[0]->numero +1;
			else $numero=1;
		  }
			
		if($etat_source[0]->transfert>=1 and $etat_dest[0]->transfert<=1 and $numero_bon){ // test si on peut faire le transfert
		  
		//  if($numero_bon){
		  	
			$id_bedget_source=$etat_source[0]->id_budget_art;
			$id_bedget_dest=$etat_dest[0]->id_budget_art;
			
			$montant_source=$etat_source[0]->solde;
			$montant_dest=$etat_dest[0]->solde;
			
			$nouveau_montant_source=$montant_source-$montant;
			$nouveau_montant_dest=$montant+$montant_dest;
			
			if($nouveau_montant_source>=0){// article source a un solde suffisant
			 $compt_trans_source=$etat_source[0]->transfert+1;
			 $compt_trans_dest=$etat_dest[0]->transfert-1;
			    $today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
				
				$this->etat_financier_mdl->transfert($id_bedget_source,$id_bedget_dest,$nouveau_montant_source,$nouveau_montant_dest,$compt_trans_source,$compt_trans_dest);
				$le_transfert=$this->etat_financier_mdl->ajout_transfert($numero,$source,$destination,$montant,$date,$nouveau_montant_source,$nouveau_montant_dest);
				$id_transfert=$le_transfert[0]->id_tranfert;
				$today = getdate();
			$time=$today[0];
			$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
			
				$num_enga_source=$this->engagement_mdl->get_engagement_numero($source);
				$num_enga_destination=$this->engagement_mdl->get_engagement_numero($destination);
				
				$num_eng_sou=$num_enga_source[0]->numero +1;
				$num_eng_dest=$num_enga_destination[0]->numero +1;
				$this->engagement_mdl->ajout_engagement_transfert($source,'1',$date,$num_eng_sou,$time,$id_transfert);
				$this->engagement_mdl->ajout_engagement_transfert($source,'2',$date,$num_eng_dest,$time,$id_transfert);
				$this->index();
				 echo(' <script>
	   window.alert(" تمت العملية بنجاح");
	   </script> ');
			}else{
				 echo(' <script>
	   window.alert(" رصيد غير كافي لاجراء العملية");
	   </script> ');
	   	$this->ajout_transfert();
			}
			
		}else{// les articles ont deja ait des transfert
			 echo(' <script>
	   window.alert("لا يمكن اجراء تحويل  ");
	   </script> ');
	   	$this->ajout_transfert();
		}
		}else{// les articles ont deja ait des transfert
			 echo(' <script>
	   window.alert("لا يمكن اجراء تحويل  ");
	   </script> ');
	   	$this->ajout_transfert();
		}
	}else{// les articles ne sont pas de meme chapitre
	
	  echo(' <script>
	   window.alert("المواد يجب ان تنتمي الى نفس القسم");
	   </script> ');
		$this->ajout_transfert();
	}
	//$this->index();
	
   }														   
	
}
